<template>
  <div style="background-color: #fff;padding: 10px;margin: 5px auto;width: 85%;align-items: center;justify-content: center;">
    <span style="line-height: 50px;font-size: 30px;color: #58e57f">药品项管理</span>
    <el-button @click="goBack" type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
  <!--  药品项搜索卡片-->
  <el-card style="margin: 5px auto;height: 70px;width: 88%">
    <el-form :inline="true">
      <el-form-item label="药品名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.label" @keydown.enter.prevent="selectMedicineOption"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="success" @click="selectMedicineOption" >搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!--    药品类列表-->
  <el-card style="margin: 1px auto;width: 88%;overflow-y:auto;">
    <div style="margin:2px 0px 40px 10px;">
      药品列表
      <el-button type="success" style="float: right;"  @click="addMedicineOption">新增药品</el-button>
    </div>
    <el-table :data="medicineOptionArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="label" label="药品名称" align="center"/>
      <el-table-column prop="value" label="药品值" align="center"/>
      <el-table-column prop="sort" label="药品排序" align="center"/>
      <el-table-column prop="price" label="药品价格" align="center"/>
      <el-table-column prop="number" label="药品数量" align="center" />
      <el-table-column prop="remark" label="备注信息" align="center" width="320"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editMedicineOption(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="deleteMedicineOption(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:30px;float: right;">
      <!--layout="total(总数), sizes(每页总数), prev(上一页), pager(当前页), next(下一页), jumper(跳转页)"
       :pager-count="4" 表示显示4个页码
       :page-sizes="[5, 10, 15, 20]" 表示一共有几个"每页显示多少条"选项
       current-page当前页码 page-size当前页码的条数 total总条数 这三个变量需要和实际数据双向绑定
       :total="400" 表示总条数，需要双向绑定
       @size-change="handleSizeChange" 表示每页显示多少条改变时触发的事件
       @current-change="handleCurrentChange" 表示当前页码改变时触发的事件 -->
      <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 15, 20]"
          v-model:current-page="currentPage"
          v-model:page-size="currentSize"
          v-model:total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </el-card>

  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="药品名称">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.label"></el-input>
      </el-form-item>
      <el-form-item label="药品值">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.value"></el-input>
      </el-form-item>
      <el-form-item label="药品排序">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="药品价格">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.price"></el-input>
      </el-form-item>
      <el-form-item label="药品数量">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.number"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="saveMedicineOptionForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveMedicineOption">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>



//返回药品类的方法
import router from "@/router";
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//定义变量保存分页器的当前页数
const currentPage = ref(1);
//定义变量保存分页器的每页显示多少条
const currentSize = ref(5);
//定义变量保存数据总条目数
const total = ref(100);
//修改每页显示多少条，触发此函数
const handleSizeChange = (val) => {
  console.log('每页展示' + val + '条数据');
  selectMedicineOption();
}
//修改当前页码，触发此函数
const handleCurrentChange = (val) => {
  console.log('当前的页数为：' + val);
  selectMedicineOption();
}
const goBack = ()=>{
  router.push("/medicine")
}
//从URL中获取查询字符串参数的药品类id值
const medicineId = new URLSearchParams(window.location.search).get('id');

// 定义数组保存药品项的表格数据的值
const medicineOptionArr = ref([]);
// 定义对象保存药品项的搜索条件
const searchForm = ref({
  label:'',
  medicineId:medicineId
});
// 定义查询药品项的方法
const selectMedicineOption = ()=>{
  searchForm.value.pageSize = currentSize.value
  searchForm.value.pageNum = currentPage.value
  let data = qs.stringify(searchForm.value)
  axios.get(BASE_URL + '/v1/medicineOption/select?'+data)
      .then((response)=>{
        if (response.data.code == 2000){
          medicineOptionArr.value = response.data.data.list
          total.value = response.data.data.total
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
onMounted(()=>{
  selectMedicineOption();
})
// 定义重置搜索的方法
const resetSearch = ()=>{
  searchForm.value = {label:'', medicineId:medicineId};
  selectMedicineOption();
}









//定义变量保存弹窗标题
const dialogTitle = ref('新增药品项');
//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
// 定义对象用来保存新增药品表单
const saveMedicineOptionForm = ref({
  medicineId:medicineId,//手动添加字典id用于药品项新增
  label:'',
  value:'',
  sort:'',
  remark:'',
  price:'',
  number:''
});


//定义关闭弹窗的方法
const handleClose = ()=>{
  if(confirm('是否关闭本弹窗?')){
    dialogVisible.value = false;
    //注意:清空新增表单时,不能清空药品类id!否则会加载出所有的药品项,而不是当前药品类下的药品项!
    saveMedicineOptionForm.value = {medicineId:medicineId, label:'', value:'', sort:'', remark:'', price:'', number:''};
  }
}

//定义新增药品的方法
const addMedicineOption = ()=>{
  dialogTitle.value = '新增药品项'
  dialogVisible.value = true;
}

// 定义保存药品的方法
const saveMedicineOption = ()=>{
  let data = qs.stringify(saveMedicineOptionForm.value)
  axios.post(BASE_URL + '/v1/medicineOption/save',data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('新增药品成功');
          selectMedicineOption();
          dialogVisible.value = false;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

// 定义编辑药品的方法
const editMedicineOption = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑药品内容'
  axios.get(BASE_URL + '/v1/medicineOption/select?id='+id)
      .then((response)=>{
        if (response.data.code == 2000){
          saveMedicineOptionForm.value = response.data.data.list[0];
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}


//定义删除药品的方法
const deleteMedicineOption = (id)=>{
  axios.post(BASE_URL + '/v1/medicineOption/delete/'+id)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('删除数据成功')
          selectMedicineOption();
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
</script>


<style scoped>

</style>